import { createVNode, render } from 'vue'
import MessageToast from '@/components/MessageToast.vue'

export type MessageType = 'success' | 'error' | 'info' | 'warning'

export interface MessageOptions {
  message: string
  type?: MessageType
  duration?: number
}

const createMessage = (options: MessageOptions) => {
  const container = document.createElement('div')
  
  const vnode = createVNode(MessageToast, {
    ...options,
    onDestroy: () => {
      render(null, container)
      document.body.removeChild(container)
    }
  })
  
  render(vnode, container)
  document.body.appendChild(container)
}

export const message = {
  success(message: string, duration?: number) {
    createMessage({ message, type: 'success', duration })
  },
  
  error(message: string, duration?: number) {
    createMessage({ message, type: 'error', duration })
  },
  
  warning(message: string, duration?: number) {
    createMessage({ message, type: 'warning', duration })
  },
  
  info(message: string, duration?: number) {
    createMessage({ message, type: 'info', duration })
  }
} 